//简单的防抖函数
function debounce(func, wait, immediate) {
  let timeout, result

  function debounced() {
    let context = this,
      args = arguments
    if (timeout) clearTimeout(timeout)
    if (immediate) {
      let callNow = !timeout
      timeout = setTimeout(function () {
        result = func.apply(context, args)
      }, wait)
      if (callNow) result = func.apply(context, args)
    } else {
      timeout = setTimeout(function () {
        result = func.apply(context, args)
      }, wait)
    }
    return result
  }

  debounced.cancel = function () {
    clearTimeout(timeout)
    timeout = null
  }
  return debounced
}

export { debounce }
